<template>
  <div class="login-container">
    <div class="login-box">
      <!--头像-->
      <div class="avatar-box">
        <img src="@/assets/avatar.jpg" />
      </div>
      <!--表单-->
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        class="login-form"
        :rules="rules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="el-icon-lock"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login";
import { getToken, setToken } from "@/util/auth";

export default {
  name: "Login",
  data() {
    return {
      // 登录表单数据
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }, //blur表示失去焦点的时候触发
          { min: 3, max: 10, message: "长度需要在3到10之间", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度需要在6到15之间", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    if (getToken()) {
      //存在token了 就不需要进入登录页面了
      this.$router.push("/layout/home");
    }
  },
  methods: {
    // 点击重置登录表单
    resetLoginForm() {
      this.$refs["loginFormRef"].resetFields();
    },
    login() {
      // 表单登录前的预校验
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        login(this.loginForm).then(
          (res) => {
            // console.log(res.data);
            if (res.data.status === 200) {
              console.log(res);
              //将Authorization放入cookie
              // setToken(res.data.Authorization);
              // alert(document.cookie);
              //存mg——name 权限验证
              // localStorage.setItem("username", this.loginForm.username);
              //跳转
              this.$router.push("/layout/home");
            } else {
              //长号密码错误
              alert(res.data.message);
            }
          },
          (err) => {
            console.log(err);
          }
        );
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  position: relative;
  height: 100vh;
  background-color: #2b4b6b;
}

.login-box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
}

.avatar-box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 10px #ddd;
  margin: 0 auto;
  transform: translateY(-50%);
  background-color: #fff;
}

.avatar-box img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #eee;
}

.login-form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
